package com.xiedafeng.www.view;

import com.xiedafeng.www.controller.RemarkController;
import com.xiedafeng.www.po.Event;
import com.xiedafeng.www.po.Remark;
import com.xiedafeng.www.po.User;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import java.util.List;

public class RemarkView {
    RemarkController remarkController=new RemarkController();
    public void showEventRemark(User user, Event event,List<Remark> remarkList,List<User> userList) {
        Stage stage=new Stage();
        GridPane gridPane=new GridPane();
        Button remarkButton=new Button("发表评论");
        gridPane.add(remarkButton,0,0);
        remarkButton.setOnMouseClicked(event1 -> {
            try {
                createRemark(user,event,stage);
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        for (int i = 1; i <= remarkList.size(); i++) {
            Remark remark=remarkList.get(i-1);
            Text name=new Text("用户 "+userList.get(i-1).getName());
            Text content=new Text("    "+remark.getRemarkContent());
            Button delete=new Button("删除");
            gridPane.add(name,0,i*4);
            gridPane.add(content,0,i*4+1,4,2);
            gridPane.add(delete,0,i*4+3);
            delete.setOnMouseClicked(event1 -> {
                try {
                    deleteRemark(user,event,remark,stage);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
        }
        gridPane.setAlignment(Pos.CENTER);
        Scene scene=new Scene(gridPane);
        stage.setScene(scene);
        stage.showAndWait();
    }
    public void showUserRemarkEvent(User user, List<Remark> remarkList, List<Event> eventList) {
        Stage stage=new Stage();
        GridPane gridPane=new GridPane();
        for (int i = 0; i < remarkList.size(); i++) {
            Event event=eventList.get(i);
            Remark remark=remarkList.get(i);
            Text publisher=new Text(event.getPublisher());
            Text createtime=new Text(event.getCreatetime());
            Text content=new Text(event.getContent());
            Text remarkContent=new Text("我的评论："+remark.getRemarkContent());
            Button delete=new Button("删除");
            gridPane.add(publisher,0,i*4);
            gridPane.add(createtime,3,i*4);
            gridPane.add(content,0,i*4+1,4,1);
            gridPane.add(remarkContent,0,i*4+2,3,1);
            gridPane.add(delete,3,i*4+3);
            delete.setOnMouseClicked(event1 -> {
                try {
                    deleteRemark(user,event,remark,stage);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
        }
        gridPane.setAlignment(Pos.CENTER);
        Scene scene=new Scene(gridPane);
        stage.setScene(scene);
        stage.showAndWait();
    }
    private void createRemark(User user, Event event, Stage stage) {
        stage.close();
        remarkController.showCreateRemarkView(user,event);
    }
    private void deleteRemark(User user, Event event, Remark remark, Stage stage) throws Exception {
        stage.close();
        remarkController.deleteRemark(user,event,remark);
    }
}
